package com.leetcode;

import com.study.common.ListNode;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-16 14:37
 * @version: 1.0
 * @description: 删除排序链表中的重复元素
 **/
public class DeleteDuplicates {
    public static void main(String[] args) {
        ListNode node6 = new ListNode(4, null);
        ListNode node4 = new ListNode(4, node6);
        ListNode node5 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node5);
        ListNode node3 = new ListNode(1, node2);
        ListNode node1 = new ListNode(1, node3);
//        ListNode node = deleteDuplicates(node1);
        ListNode node = recursive(node1);
        System.out.println(node);
    }

    private static ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;
        ListNode curr = head;
        while (null != curr.next){
            if (curr.value == curr.next.value){
                curr.next = curr.next.next;
            }else {
                curr = curr.next;
            }
        }
        return head;
    }



    private static ListNode recursive(ListNode head) {
        if (head == null || head.next == null) return head;
        head.next = recursive(head.next);
        if (head.next.value == head.value){
            return head.next;
        }
        return head;
    }

}
